﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Web;
using System.Security.Principal;
using System.Threading;
using System.Collections;
using System.Management;
using CMD_WEB.DAL;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class Group
    {
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string groupMail;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string groupName;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public int idGroup;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string idRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string dscRol;
        [DataMemberAttribute]
        public bool check;

        public static CMD.BAL.MISC.DeleteStateMsg DeleteGroup(string[] aIdGroup)
        {
            CMD.BAL.MISC.DeleteStateMsg delete;
            //convirto
            string join;
            if (aIdGroup == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(", ", aIdGroup);

            }
            //ejecuto el store
            try
            {
                int dt =CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_DELETEGROUP'" + join + "'");
                delete = new MISC.DeleteStateMsg("registros eliminados " + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new MISC.DeleteStateMsg("error al eliminar" + ex, false);
                return delete;
            }
        }

        

        public static List<Group> GetDataListGroup(string[] aIdGroup)
        {
            string join;
            if (aIdGroup == null)
            {
                join = "";   
            }
            else
            {
                 join = String.Join(", ", aIdGroup);
                
            }
            try
            {
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_SEL_GROUP'" + join + "'");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Group> list = dt.AsEnumerable().Select(
                        row => new Group
                        {
                            groupName = String.IsNullOrEmpty(row.Field<string>(1))
                            ? ""
                            : row.Field<string>(1),
                            groupMail = String.IsNullOrEmpty(row.Field<string>(2))
                            ? ""
                            : row.Field<string>(2),
                            dscRol = String.IsNullOrEmpty(row.Field<string>(4))
                            ? ""
                            : row.Field<string>(4),
                            idGroup = row.Field<int?>(0).GetValueOrDefault(),

                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static Group GetDataGroup(string aIdGroup)
        {
            try
            {
                Group group;
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_SEL_GROUP'" + aIdGroup + "'");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (aIdGroup == "")
                {
                    return null;
                }
                else
                {
                     //int a = int.Parse(dt.Rows[0]["ID_SECGROUP"].ToString());
                    //Convert.ToInt32(dt.Rows[0]["ID_SECGROUP"].ToString())
                    //int.Parse(dt.Rows[0]["ID_SECGROUP"].ToString())
                    group = new Group(Convert.ToString(dt.Rows[0]["DSC_SECGROUP"]), Convert.ToString(dt.Rows[0]["GROUPMAIL"]), Convert.ToInt32(dt.Rows[0]["ID_SECGROUP"]), Convert.ToString(dt.Rows[0]["UNID_ROL"]), Convert.ToString(dt.Rows[0]["DSC_ROL"]));
                }
                    return group;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static CMD.BAL.MISC.UpsertStateMsg UpsertGroup(Group data)
        {
            Group inser;
            String Correos= "";
            String error="";
            CMD.BAL.MISC.UpsertStateMsg insertGroup;
            try
            {
                CMD.BAL.MISC.UpsertStateMsg urs;
                List<UserData> ListaUsuarios = new List<UserData>();
                User o = new User();

                //Obtiene los datos de los usuarios del grupo Active DIrectory especificado
                try
                {
                    ListaUsuarios = o.GetListUserData(data.groupName.ToLower());
                    foreach (UserData user in ListaUsuarios)
                    {
                        Correos += user.UserMail + ";";
                    }
                    Correos = Correos.Remove(Correos.Length - 1);
                }
                catch
                {
                    error=(Correos!="")?" grupo inexistente":"";
                }
               int dt = CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_UPSERT_GROUP'" + data.groupName + "','" + Correos + "','" + data.idGroup + "'");
               inser = new Group(data.groupName, data.groupMail, data.idGroup, data.idRol, data.dscRol);
               insertGroup = new MISC.UpsertStateMsg("registros guardados "+ dt.ToString()+error, true);
               return insertGroup;
            }
            catch (Exception ex)
            {
                insertGroup = new MISC.UpsertStateMsg("error al guardar " + ex, false);
                return insertGroup;
            }
        }
        public Group()
        {
            groupMail = "";
            groupName = "";
            idGroup = 0;
            check = false;
        }
        public Group(string groupName,string groupMail, int idGroup, string idRol, string dscRol) 
        {
            this.groupName =groupName;
            this.groupMail =groupMail;
            this.idGroup = idGroup;
            this.idRol = idRol;
            this.dscRol = dscRol;
            this.check =false;
        }
    }
}
